/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2017-12-21 14:37:30
 * @version $Id$
 */

	var text = $(".searchtext").val();
	var str = localStorage.getItem("search");
	var list=new Array()

/*触摸搜索事件*/
$(".searchbtn").on("tap",function(){
	if($(".searchtext").val()!=""){
		search();
	}else{
		mui.toast('请输入搜索内容！');
	}
})

/*添加历史搜索*/	
function search(){
	var text = $(".searchtext").val();
		localStorage.setItem("search",text)
		if(localStorage.getItem("hissearch")){
			var pushlistval=localStorage.getItem("hissearch").split(",");
		 	list=pushlistval
		 }
		 	var result = $.inArray(text,list);
		 	console.log(result)

		 	for (var i = 0; i < list.length; i++) {
				if (list[i] === text) {
					list.splice(i, 1);
					break;
				}
			}

		 	list.unshift(text);

		 	localStorage.setItem("hissearch",list)
		window.location.href="search-page.html?value="+$("#searchtext").val();
}

window.addEventListener("load",function(){
	$(".searchtext").val("");
	if(localStorage.getItem("hissearch")){
		var historysearch = localStorage.getItem("hissearch")
		var lists = historysearch.split(",")
		for(var i = 0;i<lists.length;i++){
			var historicaltext = "<span class='HistoricalSearch_text' >"+ lists[i] +"</span>"
			$(".HistoricalSearch").append(historicaltext);
		}
	}else{	
	}
/*点击历史搜索*/
	var HistoricalSearch_text = $(".HistoricalSearch_text");
	for(var i=0;i<HistoricalSearch_text.length;i++){
		(function(i){
			HistoricalSearch_text.eq(i).click(function(){
				$(".searchtext").val(HistoricalSearch_text.eq(i).text())
				search()
			})		
		})(i)
	}
})
/*删除历史记录*/
document.getElementById("delete").addEventListener('tap', function() {
	var btnArray = ['否', '是'];
	mui.confirm('', '确定删除全部历史记录？', btnArray, function(e) {
		if (e.index == 1) {
			localStorage.removeItem("hissearch")
			$(".HistoricalSearch_text").hide();
		} else {
			
		}
	})
});